/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tecsup.oper.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 *
 * @author Leonel
 */
public class FuncionesERP {
    
    public int produccion (int[] cantidad, int invini, int invfin, int num){
        
        int totalproduccion = 0;
        int produccion = 0;
        
        for (int i=0; i<cantidad.length; i++){
            totalproduccion+=cantidad[i];
        }
        produccion = (totalproduccion) - invini + invfin;
        int prodmensual = (produccion/num);
        
        return prodmensual;
    }
    
    public int[] inventario (int[] demanda, int produccion, int invini, int meses){
        int inventario[] = new int [meses+1];
        inventario[0] = invini;
        
        for (int i = 1; i<=meses; i++){
            inventario[i]= produccion - demanda[i-1] + inventario[i-1];
        }
        
        return inventario;
    }
    
    public int[] empleados (int produccion, int capacidad, int num, int EmpIni){
        
        int empleados[] = new int[num+1];
        empleados[0]=EmpIni;
        for(int i=1; i<=num; i++){
            
            empleados[i] = (produccion/capacidad);
        }
        
        return empleados;
    }
    public int[] contrataciones (int[] empleados, int num){
        
        int contrataciones[] = new int[num];
        int valor;
        
        for(int i=0; i<num; i++ ){
            valor=empleados[i+1]-empleados[i];
            
            if(valor<0){
                contrataciones[i]=0;
            }else{
                contrataciones[i]=valor;
            }
        }
        return contrataciones;
    }
    public int[] despidos (int[] empleados, int num){
        
        int despidos[] = new int[num];
        int valor;
        
        for(int i=0; i<num; i++ ){
            valor=empleados[i+1]-empleados[i];
            
            if(valor>0){
                despidos[i]=0;
            }else{
                despidos[i]=(-1)*valor;
            }
        }
        return despidos;
    }
    public double[] suplementos (int produccion, int num, int capacidad){
        
        double suplementos[] = new double [num];
        double valor;
        
        for(int i = 0; i<num; i++){
            valor = (double)produccion/capacidad - produccion/capacidad;
            String val = valor+"";
            BigDecimal big = new BigDecimal(val);
            big = big.setScale(5, RoundingMode.HALF_UP);
            suplementos[i]= big.doubleValue();
        }
        return suplementos ;
    }
    
    public String costos (int num, int[] inventario , double costoInv, double costoBO
            , int[] contratos, double costoContr
            , int[] despidos,double costoDesp
            , int[] empleados, double salario
            ,double[] suplementos, double recargo
            ,int stock
            ){
        
        double costosInvBO[] = costosInventario(inventario, costoInv, costoBO, num);
        double costosContr[] = costosContratos(contratos, costoContr, num);
        double costosDesp[] = costosDespidos(despidos, costoDesp, num);
        double costosSal[] = costosSalarios(empleados, salario, num);
        double costosExt[] = costosExtras(suplementos, salario, num, recargo);
            
        
        String tabla ="";
        
        String celdasInv="";
        String celdasBO="";
        String celdasContr="";
        String celdasDesp="";
        String celdasSal="";
        String celdasExt="";
        
        double totalInv=0;
        double totalBO=0;
        double totalContr=0;
        double totalDesp=0;
        double totalSal=0;
        double totalExt=0;
        
        for (int i = 0; i<num; i++){
            if(costosInvBO[i]>=0){
                celdasInv+="<td>"+formatoDolar(costosInvBO[i])+"</td>";
                celdasBO+="<td>-</td>";
                totalInv+=costosInvBO[i];
                
            }else{
                celdasInv+="<td>0</td>";
                celdasBO+="<td>"+formatoDolar(costosInvBO[i]*(-1))+"</td>";
                totalBO+=costosInvBO[i]*(-1);
            }
            celdasContr+="<td>"+formatoDolar(costosContr[i])+"</td>";
            totalContr+=costosContr[i];
            celdasDesp+="<td>"+formatoDolar(costosDesp[i])+"</td>";
            totalDesp+=costosDesp[i];
            celdasSal+="<td>"+formatoDolar(costosSal[i])+"</td>";
            totalSal+=costosSal[i];
            celdasExt+="<td>"+formatoDolar(costosExt[i])+"</td>";
            totalExt+=costosExt[i];
            
        }
        String meses ="";
        
        for(int k=0; k<num;k++){
            meses+="<th>Mes "+(k+1)+"</th>";
        }
        String total ="";
        
        for(int n = 0; n<num-1;n++){
                total+="<td></td>";
        }
        
        double totalCostos = totalInv + totalBO + totalContr+totalDesp+totalSal+totalExt;
        total+="<th>Costo Total</th><td>"+formatoDolar(totalCostos)+"</td>";
        
        String cabezal ="<tr><td>&nbsp;</td>"+meses+"<th>Total</th></tr>";
        String filasInvBO="<tr><th>Costos Inventario</th>"+celdasInv+" <td>"+formatoDolar(totalInv)+"</td></tr>"
                + "<tr><th>Costos Escasez</th>"+celdasBO+" <td>"+formatoDolar(totalBO)+"</td></tr>";        
        String filaContr ="<tr><th>Contrataciones</th>"+celdasContr+"<td>"+formatoDolar(totalContr)+"</td></tr>";
        String filaDesp = "<tr><th>Despidos</th>"+celdasDesp+"<td>"+formatoDolar(totalDesp)+"</td></tr>";
        String filaSal = "<tr><th>Salarios</th>"+celdasSal+"<td>"+formatoDolar(totalSal)+"</td></tr>";
        String filaExt = "<tr><th>Horas Extras</th>"+celdasExt+"<td>"+formatoDolar(totalExt)+"</td></tr>";       
        String pieTabla="<tr><td></td>"+total+"</tr>";
        
        String verResultados = "<p><a class='verResultados' href='#'>Ver Resultados</a></p>";
        
        String tablaResult = resultados(totalCostos,inventario,stock);
        
        tabla = "<div style='display:none' id='costos'><table>"+cabezal+filasInvBO+filaContr+filaDesp+filaSal+filaExt+pieTabla+"</table>"+verResultados+"</div>"+tablaResult;
        
        return tabla;
    }
    
    public double[] costosInventario(int [] inventario, double costoInv, double costoBO, int num){
        double costosInventario[] = new double [num];
        for(int i = 0; i<num; i++){
            if(inventario[i+1]>=0){
                costosInventario[i] = redondear(inventario[i+1]*costoInv,2);
            }else{
                costosInventario[i] = redondear(inventario[i+1]*costoBO,2);
            }
        }
        return costosInventario;
    }
    
    public double[] costosContratos(int [] contratos, double costoContr, int num){
        
        double costosContratos[] = new double [num];
        
        for(int i = 0; i<num; i++){
            costosContratos[i] = contratos[i]*costoContr;
        }
        
        return costosContratos;
        
    }
    
    public double[] costosDespidos(int [] despidos, double costoDesp, int num){
        
        double costosDespidos[] = new double [num];
        
        for(int i = 0; i<num; i++){
            costosDespidos[i] = despidos[i]*costoDesp;
        }
        
        return costosDespidos;
        
    }
    
    public double[] costosSalarios(int [] empleados, double salario, int num){
        
        double costosSalarios[] = new double [num];
        
        for(int i = 0; i<num; i++){
            costosSalarios[i] = empleados[i+1]*salario;
        }
        
        return costosSalarios;
        
    }
    
    public double[] costosExtras(double [] suplementos, double salario, int num, double recargo){
        
        double costosExtras[] = new double [num];
        
        double p_rec = redondear(recargo/100,2) + 1;
        
        for(int i = 0; i<num; i++){
            if(suplementos[i]!=0){
            
            costosExtras[i] = salario*p_rec;
            
            }
        }       
        return costosExtras;
        
    }
    
    public String resultados(double totalCostos, int[] inventario, int stock){
        
        String tabla ="";
        
        boolean res_stock = req_stock(stock, inventario);
        String estiloStock = "";
        
        if(res_stock){
            estiloStock="<td style='background-color:#00CC33;> Si </td>'";
        }else{
            estiloStock="<td style='background-color:#FF6666;'> No </td>";
        }
        
        String cabezal = "<tr><th>Variable\\Extrategia</th><th>Producción Nivelada</th></tr>";
        
        String filaCost = "<tr><td>Costo</td><td>"+formatoDolar(totalCostos)+"</td></tr>";
        String filaInv = "<tr><td>Inventario</td>"+estiloStock+"</tr>";
        
        tabla = "<div style='display:none'><div id='resultados' align='center' style='padding:10px; background:#fff;'><table align='center'>"+cabezal+filaCost+filaInv+"</table></div></div>";
        
        return tabla;
    }
            
            
    public boolean req_stock(int stock, int[] inventario){
        boolean req_stock =true;
        int cant_inv = inventario.length;      
        for(int i = 0; i<cant_inv; i++){
            if(inventario[i]<stock){
                req_stock=false; 
                break;
            }else{
                continue;
            }            
        }       
        return req_stock;
    }
    
    public String formatoDolar(double dinero){
        
        double ddecimal = dinero - (long)dinero;
        
        String sentero = (long)dinero+"";
        String sdecimal = ddecimal+"00";

        String dolar ="$";
        
        int punto = sdecimal.indexOf(".");
        
        String decimal = sdecimal.substring(punto,punto+3);
        
        int grupos = sentero.length()/3;
        int dif = sentero.length()%3;
        String centena="";
        
        String fin = sentero.substring(dif,sentero.length());
        dolar+=sentero.substring(0,dif);
        while(grupos>0){
            centena= fin.substring(0,3);
            fin = fin.substring(3,fin.length());
            if(dif!=0){
                dolar+=","+centena;
            }else{
                dolar+=centena;
                dif+=1;
            }
            grupos+=(-1);
        }
        
        dolar+=decimal;
        return dolar;
    }
    
    public double redondear(double valor, int decimales){
        
        String val = valor+"";
        BigDecimal big = new BigDecimal(val);
        big = big.setScale(decimales, RoundingMode.HALF_UP);
        double x = big.doubleValue();
        
        return x;
        
    }
    
    public String horario (){
        
        String reloj = "";
        String mensaje = "";
        Calendar calendario = Calendar.getInstance();
        calendario = new GregorianCalendar();
        int hora, minutos, segundos;
        hora =calendario.get(Calendar.HOUR_OF_DAY);
        //minutos = calendario.get(Calendar.MINUTE);
        //segundos = calendario.get(Calendar.SECOND);
        
        if (0<=hora && hora<=11) {
            
            mensaje = "Buenos dias";
            
        } else {
            
            if (12<=hora && hora<=18) {
                
                mensaje ="Buenas tardes";
                
            } else {
                
                mensaje = "Buenas noches";
                
            }
        }
        
        //reloj = hora + ":" + minutos + ":" + segundos;
        
        
        return mensaje;
    }
    
    
    public static void main(String[] args)
    {
     
        String x = "Variable\\\\Extrategia";
        System.out.println(x);
    }

}
